<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="昵称" prop="nickName"><el-input v-model="queryParams.nickName" placeholder="请输入会员名" clearable @keyup.enter.native="handleQuery" /></el-form-item>
      <el-form-item label="手机号" prop="mobile"><el-input v-model="queryParams.mobile" placeholder="请输入会员手机号" clearable @keyup.enter.native="handleQuery" /></el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['management:member:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="memberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="头像" align="center" prop="avatarUrl" width="70">
        <template slot-scope="scope">
          <img v-if="scope.row.avatarUrl" :src="scope.row.avatarUrl" style="width: 30px; height: 30px;border-radius: 3px;" />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="会员手机号" align="center" prop="mobile" />
      <el-table-column label="注册时间" align="center" prop="registerTime"/>
      <el-table-column label="上次登录IP" align="center" prop="lastLoginIp" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['management:member:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改会员信息(游客)对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="会员名" prop="name"><el-input v-model="form.name" placeholder="请输入会员名" /></el-form-item>
        <el-form-item label="会员手机号" prop="mobile"><el-input v-model="form.mobile" placeholder="请输入会员手机号" /></el-form-item>
        <el-form-item label="会员邮箱" prop="email"><el-input v-model="form.email" placeholder="请输入会员邮箱" /></el-form-item>
        <el-form-item label="注册时间" prop="registerTime">
          <el-date-picker clearable v-model="form.registerTime" type="date" value-format="yyyy-MM-dd" placeholder="选择注册时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="上次登录时间" prop="lastLoginTime">
          <el-date-picker clearable v-model="form.lastLoginTime" type="date" value-format="yyyy-MM-dd" placeholder="选择上次登录时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="上次登录IP" prop="lastLoginIp"><el-input v-model="form.lastLoginIp" placeholder="请输入上次登录IP" /></el-form-item>
        <el-form-item label="登录次数" prop="loginNum"><el-input v-model="form.loginNum" placeholder="请输入登录次数" /></el-form-item>
        <el-form-item label="微信用户ID" prop="unionid"><el-input v-model="form.unionid" placeholder="请输入微信用户ID" /></el-form-item>
        <el-form-item label="微信openid" prop="openid"><el-input v-model="form.openid" placeholder="请输入微信openid" /></el-form-item>
        <el-form-item label="昵称" prop="nickName"><el-input v-model="form.nickName" placeholder="请输入昵称" /></el-form-item>
        <el-form-item label="会员头像" prop="avatarUrl"><el-input v-model="form.avatarUrl" type="textarea" placeholder="请输入内容" /></el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="form.gender">
            <el-radio v-for="dict in genderOptions" :key="dict.dictValue" :label="dict.dictValue">{{ dict.dictLabel }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="省" prop="province"><el-input v-model="form.province" placeholder="请输入省" /></el-form-item>
        <el-form-item label="市" prop="city"><el-input v-model="form.city" placeholder="请输入市" /></el-form-item>
        <el-form-item label="国家" prop="country"><el-input v-model="form.country" placeholder="请输入国家" /></el-form-item>
        <el-form-item label="语言" prop="language"><el-input v-model="form.language" placeholder="请输入语言" /></el-form-item>
        <el-form-item label="最后坐标lat" prop="lastLat"><el-input v-model="form.lastLat" placeholder="请输入最后坐标lat" /></el-form-item>
        <el-form-item label="最后坐标lng" prop="lastLng"><el-input v-model="form.lastLng" placeholder="请输入最后坐标lng" /></el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMember, getMember, delMember, addMember, updateMember, exportMember } from '@/api/management/member';

export default {
  name: 'Member',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员信息(游客)表格数据
      memberList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 性别字典
      genderOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: null,
        mobile: null,
        gender: null,
        orderByColumn: 'registerTime', //排序字段
        isAsc: 'desc' //排序方式sss
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: '会员名不能为空', trigger: 'blur' }],
        mobile: [{ required: true, message: '会员手机号不能为空', trigger: 'blur' }],
        registerTime: [{ required: true, message: '注册时间不能为空', trigger: 'blur' }],
        loginNum: [{ required: true, message: '登录次数不能为空', trigger: 'blur' }]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts('sys_user_sex').then(response => {
      this.genderOptions = response.data;
    });
  },
  methods: {
    /** 查询会员信息(游客)列表 */
    getList() {
      this.loading = true;
      listMember(this.queryParams).then(response => {
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 性别字典翻译
    genderFormat(row, column) {
      return this.selectDictLabel(this.genderOptions, row.gender);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        memberId: null,
        name: null,
        mobile: null,
        email: null,
        registerTime: null,
        lastLoginTime: null,
        lastLoginIp: null,
        loginNum: null,
        unionid: null,
        openid: null,
        nickName: null,
        avatarUrl: null,
        gender: '0',
        province: null,
        city: null,
        country: null,
        language: null,
        lastLat: null,
        lastLng: null
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.memberId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加会员信息(游客)';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const memberId = row.memberId || this.ids;
      getMember(memberId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '修改会员信息(游客)';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.memberId != null) {
            updateMember(this.form).then(response => {
              this.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addMember(this.form).then(response => {
              this.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const memberIds = row.memberId || this.ids;
      this.$confirm('是否确认删除会员信息(游客)编号为"' + memberIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return delMember(memberIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有会员信息(游客)数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.exportLoading = true;
          return exportMember(queryParams);
        })
        .then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    }
  }
};
</script>
